package cn.aopmin.service;

import cn.aopmin.entity.User;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @author 白豆五
 * @since 2024/7/4
 */
@Service
// @DS("master") //不加@DS注解，则使用默认数据源
public class UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insertDs1(User user) {
        // todo:自定义注解+SpringAop实现数据源的切换
        String sql = "insert into user(username,gender) values(?,?)";
        jdbcTemplate.update(sql,user.getUsername(), user.getGender());
    }

    @DS("slave1")
    public void insertDs2(User user) {
        String sql = "insert into user(username,gender) values(?,?)";
        jdbcTemplate.update(sql,user.getUsername(), user.getGender());
    }
}